package com.tanhua.server.service;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.db.UserStatus;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.vo.CommentVO;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.PublishApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.UserStatusApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author liuyp
 * @date 2021/02/01
 */
@Service
public class CommentsService {

    @Reference
    private CommentApi commentApi;
    @Reference
    private UserInfoApi userInfoApi;
    @Reference
    private PublishApi publishApi;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private MovementsMQService mqService;

    @Reference
    private UserStatusApi userStatusApi;
    /**
     * 查询某一动态的文字评论列表
     * @param movementId 动态id
     * @param page 页码
     * @param pagesize 每页几条
     */
    public ResponseEntity findCommentList(String movementId, int page, int pagesize) {
        //1. 查询动态的评论列表
        PageResult<Comment> pageResult = commentApi.findCommentList(page, pagesize, movementId);

        //2. 转换成VO
        List<Comment> commentList = pageResult.getItems();
        List<CommentVO> voList = new ArrayList<>();
        for (Comment comment : commentList) {
            CommentVO vo = new CommentVO();

            //把评论的数据封装到vo
            BeanUtils.copyProperties(comment, vo);
            vo.setId(comment.getId().toHexString());
            //  yyyy年MM月dd日 HH:mm
            String dateStr = new SimpleDateFormat("yyyy年MM月dd日 HH:mm").format(new Date(comment.getCreated()));
            vo.setCreateDate(dateStr);

            //把评论者的数据封装到vo
            UserInfo userInfo = userInfoApi.findById(comment.getUserId());
            BeanUtils.copyProperties(userInfo, vo);

            //当前用户是否对这条评论点赞了
            Boolean hasLike = redisTemplate.hasKey("comment_like_" + UserHolder.getUserId() + "_" + comment.getId().toHexString());
            vo.setHasLiked(hasLike?1:0);

            voList.add(vo);
        }

        //3. 构造返回值
        PageResult<CommentVO> voPageResult = new PageResult<>();
        BeanUtils.copyProperties(pageResult, voPageResult);
        voPageResult.setItems(voList);

        return ResponseEntity.ok(voPageResult);
    }

    public ResponseEntity saveComment(String publishId, String content) {
        Long userId = UserHolder.getUserId();
        //查询用户信息表
        UserInfo userInfo = userInfoApi.findById(userId);
        //校验用户状态

        if (userInfo.getUserStatus().equals("2")) {
            UserStatus userStatus = userStatusApi.findByUserId(userId.intValue());
            Integer freezingRange = userStatus.getFreezingRange();
            if (freezingRange == 2) {
                return ResponseEntity.status(503).body("账户评论功能已封禁,");
            }
        }


        //1.补全数据
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(publishId));
        comment.setCommentType(2);
        comment.setPubType(1);
        comment.setContent(content);
        comment.setUserId(UserHolder.getUserId());

        Publish publish = publishApi.findById(publishId);
        comment.setPublishUserId(publish.getUserId());

        //2. 保存评论
        commentApi.save(comment);

        //  发送消息给大数据推荐系统
        mqService.commentPublishMessage(publishId);

        return ResponseEntity.ok(null);
    }
}